package com.atguigu.flink.chapter07.state;

import com.atguigu.flink.bean.WaterSensor;
import com.atguigu.flink.util.KKutil;
import org.apache.flink.api.common.state.ListState;
import org.apache.flink.api.common.state.ListStateDescriptor;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

import java.util.Comparator;
import java.util.List;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/12/15 9:26
 */
public class Flink05_Keyed_State_List {
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env
                .socketTextStream("hadoop162", 9999)
                .map(line ->{
                    String[] strings = line.split(",");
                    return new WaterSensor(
                            strings[0],
                            Long.valueOf(strings[1]),
                            Integer.valueOf(strings[2]));
                })

                .keyBy(WaterSensor::getId)

                .process(new KeyedProcessFunction<String, WaterSensor, String>() {


                    private ListState<Integer> top3VcState;

                    @Override
                    public void open(Configuration parameters) throws Exception {

                        top3VcState = getRuntimeContext().getListState(new ListStateDescriptor<Integer>("liststate", Integer.class));
                    }

                    @Override
                    public void processElement(WaterSensor value,
                                               Context ctx,
                                               Collector<String> out) throws Exception {

                        top3VcState.add(value.getVc());
                        List<Integer> vcs = KKutil.toList(top3VcState.get());
                       /* vcs.sort(new Comparator<Integer>() {
                            @Override
                            public int compare(Integer o1, Integer o2) {
                                return o2.compareTo(o1);
                            }
                        });*/

                   //     vcs.sort((o1, o2) -> o2.compareTo(o1));

                        vcs.sort(Comparator.reverseOrder());
                        //干掉最后一个
                        if (vcs.size() == 4) {
                            vcs.remove(vcs.size() - 1);
                        }
                        top3VcState.update(vcs);
                        out.collect(ctx.getCurrentKey()+" top3:" + vcs.toString());

                    }
                })

                .print();


        env.execute();
    }


}
